var app = {
    querystring: null,
    initialize: function() {
        this.bind();
    },
    bind: function() {
        document.addEventListener('deviceready', this.deviceready, false);
        
        $().ready(function(){
            var url = config.url + app.querystring["slug"];
            var name = app.querystring["name"];
            
            $('.copy-link').click(function() {
                window.plugins.clipboardPlugin.setText(url);
            });

            $('.name-link').html(name);
            $('.download-link').attr('href', url);
            $('.send-link').attr('href', 'mailto:?subject=Download ' + name + 'from Rocca. Store&body=' + url)
        });
    },
    deviceready: function() {
        // note that this is an event handler so the scope is that of the event
        // so we need to call app.report(), and not this.report()
        app.report('deviceready');
    },
    report: function(id) { 
        console.log("report:" + id);
        // hide the .pending <p> and show the .complete <p>
        document.querySelector('#' + id + ' .pending').className += ' hide';
        var completeElem = document.querySelector('#' + id + ' .complete');
        completeElem.className = completeElem.className.split('hide').join('');
    }
};

(function(){
    var vars = [], hash;
    var q = document.URL.split('?')[1];
    
    if(q != undefined){
        q = q.split('&');
        for(var i = 0; i < q.length; i++){
            hash = q[i].split('=');
            vars.push(hash[1]);
            vars[hash[0]] = hash[1];
        }
    }
    app.querystring = vars;
})();